C# Confirmation Dialog Box example (ok, cancel)

chris (2004-11-16 12:01:53)
88769 views
6 replies
Somtimes you need to obtain a quick confirmation from a user to confirm an action - say a close, or delete event. This is very simple in Windows Forms UI programming, but is slightly confused by the number of Dialog classes available to the designer. In fact if all you need is a simple confirmation, you will not require a Dialog at all. All you need to do is show a MessageBox with two buttons, 'ok' and 'cancel'.

In this example, a user has clicked on a 'delete' button and I wish to confirm the deletion first:
private void button3_Click(object sender, System.EventArgs e) 
{
   if (MessageBox.Show("Really delete?","Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
   {
       // a 'DialogResult.Yes' value was returned from the MessageBox
       // proceed with your deletion
   }
}

That's it - what could be simpler?

christo

follow me on twitter: http://www.twitter.com/planet_guru

comment
virux
2008-10-15 04:22:04

quite clear

Quite clear.. and short.
thanks
reply iconedit reply
marias
2008-11-25 07:13:08

thanq

thanqso much.it helped me a lot and also tooooooo simple
reply iconedit reply
Alexei Goncharov
2009-07-17 14:38:23

LOL

You're a genious!!!
reply iconedit reply
Sarvesh
2009-08-18 11:55:58

Excellent

Somtimes you need to obtain a quick confirmation from a user to confirm an action - say a close, or delete event. This is very simple in Windows Forms UI programming, but is slightly confused by the number of Dialog classes available to the designer. In fact if all you need is a simple confirmation, you will not require a Dialog at all. All you need to do is show a MessageBox with two buttons, 'ok' and 'cancel'.

In this example, a user has clicked on a 'delete' button and I wish to confirm the deletion first:
private void button3_Click(object sender, System.EventArgs e) 
{
   if (MessageBox.Show("Really delete?","Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
   {
       // a 'DialogResult.Yes' value was returned from the MessageBox
       // proceed with your deletion
   }
}

That's it - what could be simpler?

christo

follow me on twitter: http://www.twitter.com/planet_guru

reply iconedit reply
Kashif
2010-01-07 11:58:30

nice

nice n simple !!!
reply iconedit reply
silverfall
2010-05-11 04:43:31

Short and sweet

Excellent.
Thanks a lot dude. It's great!
reply iconedit reply